package com.david.autostarter.logger.starter;

import com.david.autostarter.logger.annotation.EnableMyLogAutoConfiguration;
import com.david.autostarter.logger.annotation.MyLogScanner;
import org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

public class MyLogAutoConfigurationRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {

    private static Logger log = LoggerFactory.getLogger(MyLogAutoConfigurationRegistrar.class);
    private Environment env;
    private ResourceLoader resourceLoader;


    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
        if(log.isDebugEnabled()){
            log.debug("setEnvironment");
        }

    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
        if(log.isDebugEnabled()){
            log.debug("setResourceLoader");
        }

    }
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
                                        BeanDefinitionRegistry registry){
        if (log.isDebugEnabled()){
            log.debug("registerBeanDefinitions");
        }
        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableMyLogAutoConfiguration.class.getName()));
        String basePackage = annoAttrs.getString("basePackage");
        Class<? extends AbstractFactoryBean> implClass=annoAttrs.getClass("implClass");

        // 扫描基础包下所有带有MyLogScanner注解的接口
        MyLogClasspathScanner scanner = new MyLogClasspathScanner(registry);
        scanner.setResourceLoader(resourceLoader);
        scanner.addIncludeFilter(new AnnotationTypeFilter(MyLogScanner.class));
        // exclude package-info.java
        scanner.addExcludeFilter(new TypeFilter() {

            public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
                String className = metadataReader.getClassMetadata().getClassName();
                return className.endsWith("package-info");
            }
        });
        scanner.setFactoryBeanImplClass(implClass);
        scanner.doScan(basePackage);
    }
}
